package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.CoreLibrary;
import com.baomidou.ant.service.ICoreLibraryService;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.request.RequestAddCorePinVo;
import com.baomidou.ant.vo.request.RequestDtoVo;
import com.baomidou.ant.vo.request.RequestListCoreVo;
import com.baomidou.ant.vo.response.ResponseGetCoreVo;
import com.baomidou.ant.vo.response.ResponseListCoreVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-03-18
 */
@RestController
@RequestMapping("/core-library")
public class CoreLibraryController {
    @Autowired
    private ICoreLibraryService coreLibraryService;

    @PostMapping("/getCoreName")
    public Result getCoreName(@RequestBody RequestDtoVo requestDtoVo){
        QueryWrapper<CoreLibrary> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("dept_id", requestDtoVo.getDeptId());
        queryWrapper.eq("core_status", 1);
        List<CoreLibrary> coreList=coreLibraryService.list(queryWrapper);
        List<ResponseGetCoreVo> voList=new ArrayList<>();
        for(CoreLibrary coreLibrary:coreList){
            ResponseGetCoreVo vo=new ResponseGetCoreVo();
            BeanUtils.copyProperties(coreLibrary,vo);
            voList.add(vo);
        }
        return Result.getSuccess().setData(voList);
    }

    @PostMapping("/listCore")
    public Result listCore(@RequestBody RequestListCoreVo vo){
        IPage<ResponseListCoreVo> iPage=coreLibraryService.listCore(vo);
        return Result.getSuccess().setData(iPage);
    }

    @GetMapping("/banCore/{coreId}")
    public Result banCore(@PathVariable String coreId){
        CoreLibrary coreLibrary=coreLibraryService.getById(Integer.parseInt(coreId));
        if(coreLibrary.isCoreStatus()){
            coreLibrary.setCoreStatus(false);
            coreLibraryService.updateById(coreLibrary);
            return Result.getSuccess().setMsg("停用成功");
        }else {
            coreLibrary.setCoreStatus(true);
            coreLibraryService.updateById(coreLibrary);
            return Result.getSuccess().setMsg("启用成功");
        }
    }

    @PostMapping("/addCore")
    public Result addCore(@RequestBody RequestAddCorePinVo vo){
        if(ObjectUtils.isNotEmpty(coreLibraryService.queryByCorePin(vo.getCorePin(),vo.getDeptId()))){
            return Result.getFailure().setMsg("新增失败</br>该部门下存在相同的core品名>");
        }
        CoreLibrary coreLibrary=new CoreLibrary();
        BeanUtils.copyProperties(vo,coreLibrary);
        String coreCode= MyStringUtil.getPinYinHeadChar(vo.getCorePin());
        coreCode=coreCode+"-"+vo.getCode()+"-";
        String codeKey=MyStringUtil.getRandomStringArray(6,1)[0];
        //判断生成的key是否重复
        for(int i=0;i<coreLibraryService.list().size();i++){
            if(ObjectUtils.isNotEmpty(coreLibraryService.queryByCoreCode(codeKey))){
                codeKey=MyStringUtil.getRandomStringArray(6,1)[0];
            }else
                break;
        }
        coreLibrary.setCoreCode(coreCode+codeKey);
        coreLibrary.setCoreStatus(true);
        if(coreLibraryService.save(coreLibrary)){
            return Result.getSuccess().setMsg("新增成功");
        }else
            return Result.getFailure().setMsg("新增失败");
    }

}
